package com.guigu.work.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.base.entity.CoreOrg;
import com.guigu.base.entity.GlobalUser;
import com.guigu.common.enums.driverjob.DriverJobStatus;
import com.guigu.work.dto.DriverJobDTO;
import com.guigu.work.entity.DriverJob;
import com.guigu.work.entity.TaskTransport;
import com.guigu.work.feign.BaseFeign;
import com.guigu.work.mapper.DriverJobMapper;
import com.guigu.work.mapper.TaskTransportMapper;
import com.guigu.work.service.DriverJobService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 司机作业单 服务实现类
 * </p>
 *
 * @author liAo
 * @since 2021-11-09
 */
@Slf4j
@Transactional
@Service
public class DriverJobServiceImpl extends ServiceImpl<DriverJobMapper, DriverJob> implements DriverJobService {

    private TaskTransportMapper taskTransportMapper;
    private BaseFeign baseFeign;

    public DriverJobServiceImpl(TaskTransportMapper taskTransportMapper, BaseFeign baseFeign) {
        this.taskTransportMapper = taskTransportMapper;
        this.baseFeign = baseFeign;
    }

    /**
     * 新增司机作业单
     *
     * @param dto@return 作业单信息
     */
    @Override
    public DriverJob saveDriverJob(DriverJobDTO dto) {
        if (null == dto) {
            throw new IllegalArgumentException("参数异常!");
        }
        /**
         * 修改之前运输任务的状态
         */
        if (StringUtils.isNotBlank(dto.getTaskTransportId())) {
            TaskTransport taskTransport = new TaskTransport();
            taskTransport.setAssignedStatus(2);
            taskTransport.setId(dto.getTaskTransportId());
            taskTransportMapper.updateById(taskTransport);
        }
        //status
        dto.setStatus(DriverJobStatus.PENDING.getCode());
        //time
        dto.setCreateTime(new Date());
        this.saveOrUpdate(dto);
        log.info("保存成功ID:" + dto.getId());
        return dto;
    }

    /**
     * 修改司机作业单
     *
     * @param dto 作业单信息
     * @return 作业单信息
     */
    @Override
    public DriverJob updateDriverJob(DriverJobDTO dto) {
        if (null == dto) {
            throw new IllegalArgumentException("参数异常!");
        }
        this.updateById(dto);
        return dto;
    }

    /**
     * id-status-taskTransportId-driverId
     *
     * @param dto
     * @return
     */
    @Override
    public IPage<DriverJob> findByPage(DriverJobDTO dto) {
        if (null == dto) {
            throw new IllegalArgumentException("参数异常!");
        }
        Page<DriverJob> page = new Page<DriverJob>(dto.getPage(), dto.getPageSize());

        LambdaQueryWrapper<DriverJob> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(dto.getId())) {
            wrapper.like(DriverJob::getId, dto.getId());
        }
        if (StringUtils.isNotEmpty(dto.getDriverId())) {
            wrapper.eq(DriverJob::getDriverId, dto.getDriverId());
        }
        if (dto.getStatus() != null) {
            wrapper.eq(DriverJob::getStatus, dto.getStatus());
        }
        if (StringUtils.isNotEmpty(dto.getTaskTransportId())) {
            wrapper.like(DriverJob::getTaskTransportId, dto.getTaskTransportId());
        }

        return this.page(page, wrapper);
    }

    /**
     * 获取司机作业单列表
     *
     * @param dto
     * @return 司机作业单列表
     */
    @Override
    public List<DriverJobDTO> findAll(DriverJobDTO dto) {
        if (null == dto) {
            throw new IllegalArgumentException("参数异常!");
        }
        //司机作业单id列表
        LambdaQueryWrapper<DriverJob> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (null != dto.getIds() && dto.getIds().size() > 0) {
            lambdaQueryWrapper.in(DriverJob::getId, dto.getIds());
        }
        //司机作业单Id
        if (StringUtils.isNotEmpty(dto.getId())) {
            lambdaQueryWrapper.like(DriverJob::getId, dto.getId());
        }
        //司机作业单状态
        if (dto.getStatus() != null) {
            lambdaQueryWrapper.eq(DriverJob::getStatus, dto.getStatus());
        }
        //driverId
        if (StringUtils.isNotEmpty(dto.getDriverId())) {
            lambdaQueryWrapper.like(DriverJob::getDriverId, dto.getDriverId());
        }
        //taskTransportId
        if (StringUtils.isNotEmpty(dto.getTaskTransportId())) {
            lambdaQueryWrapper.like(DriverJob::getTaskTransportId, dto.getTaskTransportId());
        }
        //创建时间降序
        lambdaQueryWrapper.orderByDesc(DriverJob::getCreateTime);
        List<DriverJob> jobList = this.list(lambdaQueryWrapper);

        /**
         * 使用stream流开始映射,将id替换成数据
         */
        List<DriverJobDTO> collect = jobList.stream().map(d -> {
            DriverJobDTO driverJobDTO = new DriverJobDTO();
            BeanUtils.copyProperties(d, driverJobDTO);
            CoreOrg startAgency = baseFeign.findCoreOrgById(d.getStartAgencyId()).getData();
            CoreOrg endAgency = baseFeign.findCoreOrgById(d.getEndAgencyId()).getData();
            GlobalUser driverUser = baseFeign.findGlobalUserById(d.getDriverId()).getData();
            if (ObjectUtils.isNotEmpty(startAgency)) {
                driverJobDTO.setStartAgencyName(startAgency.getName());
            }
            if (ObjectUtils.isNotEmpty(endAgency)) {
                driverJobDTO.setEndAgencyName(endAgency.getName());
            }
            if (ObjectUtils.isNotEmpty(driverUser)) {
                driverJobDTO.setDriverName(driverUser.getName());
            }
            return driverJobDTO;
        }).collect(Collectors.toList());
        return collect;
    }
}
